package com.lyy.photovoltaic.service.impl;

import com.lyy.photovoltaic.beans.Equipments;
import com.lyy.photovoltaic.common.vo.ResultCode;
import com.lyy.photovoltaic.common.vo.ResultVO;
import com.lyy.photovoltaic.mapper.EquipmentsMapper;
import com.lyy.photovoltaic.service.EquipmentsService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

@Service
public class EquipmentsServiceImpl implements EquipmentsService {
    @Autowired
    EquipmentsMapper equipmentsMapper;

    @Override
    public ResultVO findEquipmentsByPages() {
//分页查询
        int pageNum = 2;
        int pageSize = 6;
        int start = (pageNum - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(start, pageSize);
        List<Equipments> equipments = equipmentsMapper.selectByRowBounds(new Equipments(), rowBounds);
//        for (Equipments equipment : equipments) {
//            System.out.println(equipment);
//        }
        //查询总记录数
        int i = equipmentsMapper.selectCount(new Equipments());
//        System.out.println(i);
        Object[] obj = new Object[]{equipments, i};


        ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, obj);
        return resultVO;
    }

    @Override
    public ResultVO addEquipment(String eId,Equipments equipment) {
        //增加设备
        //System.out.println(eId);
        System.out.println(equipment);
        Equipments key = equipmentsMapper.selectByPrimaryKey(eId);
        //System.out.println(key);
        if (key == null) {
            equipment.seteCreatetime(new Date());

//            equipment.setImageUrl();
            int i = equipmentsMapper.insert(equipment);
            if (i > 0) {
                //System.out.println(i);
                ResultVO resultVO = new ResultVO(ResultCode.INSER_SUCCESS, equipment);
                return resultVO;
            }

        }
        ResultVO resultVO2 = new ResultVO(ResultCode.INSER_FAILED, equipment);
        return resultVO2;
    }

    @Override
    public ResultVO getEquipment(String eId) {
//        System.out.println(eId);
        Equipments equipment = equipmentsMapper.selectByPrimaryKey(eId);
//        System.out.println(equipment);
        if (equipment!=null) {
        ResultVO resultVO = new ResultVO(ResultCode.SUCCESS,"查询成功", equipment);
        return resultVO;
        }
        return new ResultVO(ResultCode.FAILED,"不存在该设备", null);
    }

    @Override
    public ResultVO findEquipmentsList(String[] strings) {
        System.out.println(strings);
        List<String> strings1 = Arrays.asList(strings);
        System.out.println(strings1);
        List<Equipments> equipmentsLists = equipmentsMapper.findEquipmentsList(strings);
        System.out.println(equipmentsLists);
        if (equipmentsLists.size()>0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS,"查询成功", equipmentsLists);
            return resultVO;
        }

        return new ResultVO(ResultCode.FAILED,"查询失败", null);
    }

    @Override
    public ResultVO findAllEquipments() {

        List<Equipments> allEquipments = equipmentsMapper.selectAll();
//        for (Equipments equipment : allEquipments) {
//            System.out.println(equipment);
//        }
        ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, allEquipments);
        return resultVO;
    }

    @Override
    public ResultVO deleteEquipments(String eId) {
        int i = equipmentsMapper.deleteByPrimaryKey(eId);
        if (i > 0) {
            return new ResultVO(ResultCode.SUCCESS, "删除成功",null);
        }
        return new ResultVO(ResultCode.NO,"删除失败",null);
    }

    @Override
    public ResultVO updateEquipment(Equipments equipment) {
        int i = equipmentsMapper.updateByPrimaryKey(equipment);
//        System.out.println(i);
        if (i > 0) {
            return new ResultVO(ResultCode.SUCCESS, "更新成功",equipment);
        }
        return new ResultVO(ResultCode.FAILED,"更新失败",equipment);
    }

    @Override
    public ResultVO findEquipmentByCategory(String categoryId) {
        Example example=new Example(Equipments.class);//要查询的表对应的实体类
        Example.Criteria criteria=example.createCriteria();//创建查询标准
        criteria.andEqualTo("categoryId",categoryId);//调用方法，编写自己想要查询的条件
        List<Equipments> equipments = equipmentsMapper.selectByExample(example);
        ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, equipments);
        return resultVO;
    }

    @Override
    public ResultVO findEquipmentsByExample(String input) {
//        System.out.println(input);
        Example example=new Example(Equipments.class);//要查询的表对应的实体类
        Example.Criteria criteria=example.createCriteria();//创建查询标准
//        criteria.
        criteria.andLike("eName","%" + input + "%");//调用方法，编写自己想要查询的条件
        List<Equipments> equipments = equipmentsMapper.selectByExample(example);
        ResultVO resultVO = new ResultVO(ResultCode.SUCCESS,"根据输入条件查询商品", equipments);
        return resultVO;
    }

    @Override
    public ResultVO findEquipmentsByName(String name) {
//        System.out.println(name);
        Example example = new Example(Equipments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("eName", name);
//        List<Equipments> equipments = equipmentsMapper.selectByExample(example);
        Equipments equipment = equipmentsMapper.selectOneByExample(example);
        if (equipment == null) {
            return new ResultVO(ResultCode.FAILED,"查找设备详情失败",null);
        }
        ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "通过点击查看设备详细信息",equipment);
        return resultVO;
    }

    @Override
    public ResultVO findEquipmentsByImageUrl(String imageUrl) {
//        System.out.println(imageUrl);
        Example example = new Example(Equipments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("imageUrl", imageUrl);
//        List<Equipments> equipments = equipmentsMapper.selectByExample(example);
        Equipments equipment = equipmentsMapper.selectOneByExample(example);
        if (equipment == null) {
            return new ResultVO(ResultCode.FAILED,"查找设备详情失败",null);
        }
        ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "通过图片查看设备详细信息",equipment);
        return resultVO;
    }

    @Override
    public ResultVO hotEquipmentsList(int pageNum) {
        Example example = new Example(Equipments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andGreaterThanOrEqualTo("eConuts",1);
//        int pageNum = 1;
        int pageSize = 4;
        int start = (pageNum - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(start, pageSize);
        List<Equipments> equipments = equipmentsMapper.selectByExampleAndRowBounds(example,rowBounds);
        if (equipments.size() > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "查看热门信息",equipments);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED,"查看热门信息失败",null);

    }

    @Override
    public ResultVO indexEquipmentsList() {
        Example example = new Example(Equipments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("categoryId",54);
        int pageNum = 1;
        int pageSize = 4;
        int start = (pageNum - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(start, pageSize);
        List<Equipments> equipments = equipmentsMapper.selectByExampleAndRowBounds(example,rowBounds);
        if (equipments.size() > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "查看首页信息",equipments);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED,"查看首页信息失败",null);
    }

    @Override
    public ResultVO transformerEquipmentsList() {
        Example example = new Example(Equipments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("categoryId",11);
        int pageNum = 1;
        int pageSize = 4;
        int start = (pageNum - 1) * pageSize;
        RowBounds rowBounds = new RowBounds(start, pageSize);
        List<Equipments> equipments = equipmentsMapper.selectByExampleAndRowBounds(example,rowBounds);
        System.out.println(equipments);
        if (equipments.size() > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "查看首页信息",equipments);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED,"查看首页信息失败",null);
    }


}
